package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.crm.dto.LogListRequest;
import com.jmp.api.crm.dto.LogListResponse;
import com.jmp.autocofiguration.web.interceptor.LogOperation;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CompanyApiCrmMapper;
import one.stand.model.AdminRecordModel;
import one.stand.model.ResultModel;
import one.stand.util.DateUtil;
import one.stand.util.RequestContextHolder;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "操作记录")
@RestController
@RequestMapping("crmPc/log")
@Slf4j
public class LogController {
    @Autowired
    private CompanyApiCrmMapper companyApiCrmMapper;

    @ApiOperation(value = "查询操作记录")
    @LogOperation(value = "查询操作记录")
    @PostMapping("list")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<LogListResponse> selectAdminRecordList(@RequestAttribute LogListRequest request) {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        String nameLike = SqlUtil.like(request.getName());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AdminRecordModel> list = companyApiCrmMapper.selectLog(userCompanyId, nameLike);
        PageResponse2<LogListResponse.Log> logs = ConvertUtils.page2(list, model -> LogListResponse.Log.builder()
                .name(model.getName())
                .content(model.getContent())
                .time(DateUtil.format(DateUtil.formatter, model.getCreateTime()))
                .build());
        LogListResponse response = LogListResponse.builder()
                .logs(logs)
                .build();
        return ResultModel.success(response);
    }
}
